package {
	import com.ricardoteix.subtitle.events.FileParserEvent;
	import com.ricardoteix.subtitle.events.SubtitleEvent;
	import com.ricardoteix.subtitle.parsers.FileParser;
	import com.ricardoteix.subtitle.Subtitle;
	import com.ricardoteix.subtitle.SubtitleBlock;
	import com.ricardoteix.utils.timer.ChronoTrigger;
	import com.ricardoteix.utils.timer.events.ChronoTriggerEvent;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	
	/**
	 * ...
	 * @author Ricardo Teixeira
	 */
	public class Main extends Sprite {
		private var campo:TextField;
		private var subtitleBlocks:Vector.<SubtitleBlock>;
		
		private var subtitle:Subtitle;
		
		public function Main():void {
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void {
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			this.subtitle = new Subtitle ();
			this.subtitle.addEventListener (SubtitleEvent.ENTER_BLOCK, onEnterBlock);
			this.subtitle.addEventListener (SubtitleEvent.EXIT_BLOCK, onExitBlock);
			this.subtitle.addEventListener (SubtitleEvent.UPDATE, onUpdate);
			this.subtitle.addEventListener (SubtitleEvent.EOF, onEOF);
			
			this.campo = new TextField ();
			this.campo.width = this.stage.stageWidth;
			this.campo.height = this.stage.stageHeight;
			
			this.addChild (this.campo);
			
			this.stage.addEventListener (KeyboardEvent.KEY_DOWN, onKeyDown);
		}
		
		private function onKeyDown(e:KeyboardEvent):void {
			if (e.keyCode == Keyboard.SPACE) {
				this.subtitle.togglePause ();
			}
		}
		
		private function onEOF(e:SubtitleEvent):void {
			trace ("onEOF");
		}
		
		private function onEnterBlock(e:SubtitleEvent):void {
			this.campo.text = e.currentTime + "\n" + e.block.text
		}
		
		private function onExitBlock(e:SubtitleEvent):void {
			this.campo.text = "";
		}
		
		private function onUpdate(e:SubtitleEvent):void {
			// trace (e.currentTime);
		}
		
	}
	
}